package com.cwz.gulimall.member.service.impl;

import com.cwz.gulimall.member.entity.MemberLevelEntity;
import com.cwz.gulimall.member.exception.MobileExistException;
import com.cwz.gulimall.member.exception.UserNameExistException;
import com.cwz.gulimall.member.service.MemberLevelService;
import com.cwz.gulimall.member.vo.MemberLoginVo;
import com.cwz.gulimall.member.vo.MemberRegisterVo;
import com.cwz.gulimall.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cwz.gulimall.common.utils.PageUtils;
import com.cwz.gulimall.common.utils.Query;

import com.cwz.gulimall.member.dao.MemberDao;
import com.cwz.gulimall.member.entity.MemberEntity;
import com.cwz.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    MemberLevelService memberLevelService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public void register(MemberRegisterVo vo) {
        MemberEntity memberEntity = new MemberEntity();
        MemberLevelEntity memberLevelEntity = memberLevelService.getDefaultMemberLevel();
        memberEntity.setLevelId(memberLevelEntity.getId());
        checkMobileIfUnique(vo.getMobile());
        checkUserNameIfUnique(vo.getUserName());
        memberEntity.setMobile(vo.getMobile());
        memberEntity.setUsername(vo.getUserName());
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String password = passwordEncoder.encode(vo.getPassword());
        memberEntity.setPassword(password);
        memberEntity.setNickname(vo.getUserName());
        this.baseMapper.insert(memberEntity);
    }

    @Override
    public void checkMobileIfUnique(String mobile) throws MobileExistException {
        Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile",mobile));
        if (count > 0) {
            throw new  MobileExistException();
        }
    }

    @Override
    public void checkUserNameIfUnique(String userName) throws UserNameExistException {
        Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", userName));
        if (count > 0) {
            throw new UserNameExistException();
        }
    }

    @Override
    public MemberEntity login(MemberLoginVo vo) {
        MemberEntity memberEntity = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", vo.getLoginacct()).or().eq("mobile", vo.getLoginacct()));
        if (memberEntity != null) {
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            boolean matches = passwordEncoder.matches(vo.getPassword(), memberEntity.getPassword());
            if (matches) {
                return memberEntity;
            }
        }
        return null;
    }

    @Override
    public MemberEntity oauthLogin(SocialUser socialUser) {
        MemberEntity memberEntity = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("social_uid", socialUser.getUid()));
        if (memberEntity != null) {
            memberEntity.setAccessToken(socialUser.getAccess_token());
            memberEntity.setExpiresIn(socialUser.getExpires_in());
            this.baseMapper.updateById(memberEntity);
            return memberEntity;
        } else {
            MemberEntity registerMember = new MemberEntity();
            registerMember.setUsername(socialUser.getNickName());
            registerMember.setExpiresIn(socialUser.getExpires_in());
            registerMember.setAccessToken(socialUser.getAccess_token());
            registerMember.setCreateTime(new Date(Long.parseLong(socialUser.getCreated_at())));
            registerMember.setSocialUid(socialUser.getUid());
            registerMember.setNickname(socialUser.getNickName());
            this.baseMapper.insert(registerMember);
            return registerMember;
        }
    }
}